JSON


JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is commonly used for transmitting data in web applications.

JSON Syntax

JSON syntax is derived from JavaScript object notation syntax:

Example

Here is an example of a JSON object:

{
    "firstName": "John",
    "lastName": "Doe",
    "age": 30,
    "address": {
        "street": "123 Main St",
        "city": "New York",
        "state": "NY"
    },
    "phoneNumbers": [
        { "type": "home", "number": "212-555-1234" },
        { "type": "office", "number": "646-555-4567" }
    ]
}

Converting JSON to JavaScript Object

You can convert a JSON string into a JavaScript object using the JSON.parse() method:

let jsonString = '{"firstName":"John","lastName":"Doe","age":30}';
let jsonObject = JSON.parse(jsonString);
console.log(jsonObject.firstName); // John

Converting JavaScript Object to JSON

You can convert a JavaScript object into a JSON string using the JSON.stringify() method:

let jsObject = { firstName: "John", lastName: "Doe", age: 30 };
let jsonString = JSON.stringify(jsObject);
console.log(jsonString); // {"firstName":"John","lastName":"Doe","age":30}

Use Cases

JSON is commonly used for:

For more detailed information, you can check out resources like W3Schools[^1^][1] and MDN Web Docs[^2^][3].